package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.util.JxcException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    @Override
    public Map<String, Object> queryPageGoods(Integer pageNum, Integer pageSize, String codeOrName, Integer goodsTypeId) {
        //自己手动的写分页的功能
        //1.查询对应当前页的行数据
        // limit m,n 跳过m个,显示n个    m = (pageNum - 1) * pageSize
        Integer jumpCount = (pageNum - 1) * pageSize;
        List<Goods> pagePoods = goodsDao.queryPageGoods(jumpCount,pageSize,codeOrName,goodsTypeId);
        //2.查询总的记录数
        Integer total = goodsDao.queryTotalCount(codeOrName,goodsTypeId);
        //3.封装查询的结果并返回
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",pagePoods);
        return map;
    }

    @Override
    public Map<String, Object> queryPageGoodsByNameAndTypeId(Integer pageNum, Integer pageSize, String goodsName, String goodsTypeId) {

        //2.查询对应当前页的行数据
        // limit m,n 跳过m个,显示n个    m = (pageNum - 1) * pageSize
        Integer jumpCount = (pageNum - 1) * pageSize;
        List<Goods> pagePoods = goodsDao.queryPageGoodsByNameAndTypeId(jumpCount,pageSize,goodsName,goodsTypeId);
        //3.查询总的记录数
        Integer total = goodsDao.queryTotalCountByNameAndTypeId();
        //4.封装查询的结果并返回
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",pagePoods);
        return map;
    }

    @Override
    public void saveOrUpdate(Goods goods, Integer goodsId) {
        if (goods == null){
            new JxcException("商品不存在!!!");
        }
        if (goods.getGoodsId() == null){
            //进来这里面说明商品的id不存在,那么就应该执行新增的操作
            //有三个属性前端没有传值过来,字段还不能删除,不然的话就提示字段数量不匹配
            goods.setInventoryQuantity(goods.getMinNum());
            goods.setLastPurchasingPrice(goods.getPurchasingPrice());
            goods.setState(0);
            goodsDao.save(goods);
        }else{
            goods.setGoodsId(goodsId);
            //进来这里面说明商品的id存在,执行的应该是修改的操作
            goodsDao.update(goods);
        }
    }

    @Override
    public ServiceVO deleteById(Integer goodsId) {
        //要判断商品状态,入库1、有进货和销售单据2的不能删除,那么就只剩下的状态为0的可以删除了
        //所以需要先查询出来对应的结果
        Goods good = goodsDao.queryById(goodsId);
        if (good.getState() == 1 || good.getState() == 2){
            return new ServiceVO<>(ErrorCode.HAS_FORM_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS);
        }
        goodsDao.deleteById(goodsId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> queryNoInventoryQuantity(Integer pageNum, Integer pageSize, String nameOrCode) {
        //2.查询对应当前页的行数据
        // limit m,n 跳过m个,显示n个    m = (pageNum - 1) * pageSize
        Integer jumpCount = (pageNum - 1) * pageSize;
        List<Goods> pagePoods = goodsDao.queryNoInventoryQuantity(jumpCount,pageSize,nameOrCode);
        //3.查询总的记录数
        Integer total = goodsDao.queryNoInventoryQuantityTotal(nameOrCode);
        //4.封装查询的结果并返回
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",pagePoods);
        return map;
    }

    @Override
    public Map<String, Object> queryHasInventoryQuantity(Integer pageNum, Integer pageSize, String nameOrCode) {
        //2.查询对应当前页的行数据
        // limit m,n 跳过m个,显示n个    m = (pageNum - 1) * pageSize
        Integer jumpCount = (pageNum - 1) * pageSize;
        List<Goods> pagePoods = goodsDao.queryHasInventoryQuantity(jumpCount,pageSize,nameOrCode);
        //3.查询总的记录数
        Integer total = goodsDao.queryHasInventoryQuantityTotal(nameOrCode);
        //4.封装查询的结果并返回
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",pagePoods);
        return map;
    }

    @Override
    public void saveStockOrQuantityOrPrice(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.saveStockOrQuantityOrPrice(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        //要判断商品状态,入库1、有进货和销售单据2的不能删除,那么就只剩下的状态为0的可以删除了
        //所以需要先查询出来对应的结果
        Goods good = goodsDao.queryById(goodsId);
        if (good.getState() == 1 || good.getState() == 2){
            return new ServiceVO<>(ErrorCode.HAS_FORM_ERROR_CODE,ErrorCode.HAS_FORM_ERROR_MESS);
        }
        //检查确认没有问题之后就可以将商品的库存的数量修改为0了
        goodsDao.deleteStock(goodsId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> listAlarm() {
        //查询所有 当前库存量 小于 库存下限的商品信息
        List<Goods> goods = goodsDao.listAlarm();
        Map<String,Object> map = new HashMap<>();
        map.put("rows",goods);
        return map;
    }
}
